<template>
	<!-- 工站库存详情 -->
	<div class="content">
		<div class="pageSearchView">
			<el-form :inline="true" :model="searchParam" class="demo-form-inline" :rules="formRules" ref="searchForm">
				<el-form-item label="产品编码" style="width: 250px" prop="productCode">
					<el-select v-model="searchParam.productCode" filterable clearable style="width: 200px">
						<el-option v-for="item in productData" :key="item.productCode" :value="item.productCode"
											 :label="item.productCode" />
					</el-select>

				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="submitForm()">查询</el-button>
					<el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="pageTableView">
			<div class="pageTableMenuView">
				<div class="pageTableTitle">列表信息</div>
				<div class="pageTableButView">
					<!--					按钮-->
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="paginatedData" :span-method="arraySpanMethod" border style="width: 100%"
								height="calc(100vh - 325px)">
				<el-table-column type="index" width="60" align="center"></el-table-column>
				<el-table-column prop="productCode" label="产品编码" min-width="180" align="center"
												 show-overflow-tooltip></el-table-column>
				<el-table-column prop="procedureName" label="工序" min-width="180" align="center"
												 show-overflow-tooltip></el-table-column>
				<!--				<el-table-column prop="materialStatus" label="状态" min-width="180" align="center" show-overflow-tooltip>-->
				<!--					<template #default="{ row }">-->
				<!--						{{ getStatusLabel(row.materialStatus) }}-->
				<!--					</template>-->
				<!--				</el-table-column>-->
				<el-table-column prop="inMaterialCode" label="投入物料" min-width="200" align="center"
												 show-overflow-tooltip></el-table-column>
				<el-table-column prop="outMaterialCode" label="产出物料编码" min-width="180" align="center"
												 show-overflow-tooltip></el-table-column>
				<el-table-column prop="storageCode" label="库位编码" min-width="180" align="center"></el-table-column>
				<el-table-column prop="notStatusCount" label="未加工数量" min-width="180" align="center"></el-table-column>
				<el-table-column prop="passCount" label="合格数量" min-width="180" align="center"></el-table-column>
				<el-table-column prop="scrappedCount" label="报废数量" min-width="180" align="center"></el-table-column>
				<el-table-column prop="qualifiedCount" label="不合格数量" min-width="180" align="center"></el-table-column>
			</el-table>
			<div class="pagePagination">
				<el-pagination background v-model:current-page="pageSearch.current"
											 v-model:page-size="pageSearch.pageSize" :page-sizes="[10, 20, 50, 100, 200, 500]"
											 layout="total, sizes, prev, pager, next" :total="pageTotal" @size-change="handleSizeChange"
											 @current-change="handleCurrentChange" />
			</div>
		</div>
	</div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref, onMounted } from 'vue';
import { ElMessage, ElForm } from 'element-plus';
import { ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search, Download } from '@element-plus/icons-vue';
import dayjs from 'dayjs';

const searchForm = ref<InstanceType<typeof ElForm>>();
const pageSearch = ref({
	current: 1,
	pageSize: 10
});
const productData = ref([]);
const pageTotal = ref(null); // 页面总数
const searchParam = ref({
	year: new Date().getFullYear().toString()
});


const formRules = ref({});

const getSpanArr = (data) => {
	let spanArr = [];
	let spanArr1 = [];
	let pos = 0;
	let pos1 = 0;

	for (let i = 0; i < data.length; i++) {
		if (i === 0) {
			spanArr.push(1);
			spanArr1.push(1);
			pos = 0;
			pos1 = 0;
		} else {
			// 检查物料名称是否相同
			if (data[i].productId === data[i - 1].productId) {
				spanArr[pos] += 1;
				spanArr.push(0);
				// if (data[i].materialStatus === data[i - 1].materialStatus) {
				// 	spanArr1[pos1] += 1;
				// 	spanArr1.push(0);
				// } else {
				// 	spanArr1.push(1);
				// 	pos1 = i;
				// }
				// 检查工序名称是否相同
			} else {
				spanArr.push(1);
				pos = i;
				spanArr1.push(1);
				pos1 = i;
			}
		}
	}
	return [spanArr, spanArr1];
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
	// 获取合并信息数组
	const [spanArr, spanArr1] = getSpanArr(paginatedData.value);

	if (columnIndex === 1) { // 物料列
		const _row = spanArr[rowIndex];
		const _col = _row > 0 ? 1 : 0;
		return {
			rowspan: _row,
			colspan: _col
		};
	}

	// if (columnIndex === 2 || columnIndex === 3) { // 物料列
	// 	const _row = spanArr1[rowIndex];
	// 	const _col = _row > 0 ? 1 : 0;
	// 	return {
	// 		rowspan: _row,
	// 		colspan: _col
	// 	};
	// }
};


const paginatedData = ref([]);
const getList = () => {
	axiosInstance.post('/api/warehouseStorageMaterial/productWarehouseReport', {
		...searchParam.value,
		...pageSearch.value
	})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			paginatedData.value = res.data.records;
			pageTotal.value = res.data.total;
		});
};

const statusList = ref([{
	label: '不可用',
	value: 0
}, {
	label: '可用',
	value: 1
}, {
	label: '运输中',
	value: 2
}, {
	label: '生产锁定',
	value: 3
}, {
	label: '不合格',
	value: 4
}, {
	label: '报废',
	value: 5
}]);
const getStatusLabel = (value) => {
	const status = statusList.value.find(s => s.value === value);
	return status ? status.label : '-';
};

const handleSizeChange = (e) => {
	pageSearch.value.pageSize = e;
	getList();
};

const handleCurrentChange = (e) => {
	pageSearch.value.current = e;
	getList();
};

// 提交表单
const submitForm = () => {
	searchForm.value?.validate((valid) => {
		if (valid) {
			getList();
		}
	});
};

// 重置搜索条件
const resetSearch = () => {
	searchParam.value = {};
	pageSearch.value.current = 1;
	getList();
};
const getOption = () => {
	axiosInstance.post('/api/basedataProduct/getAll', {})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			productData.value = res.data;
		});
};

onMounted(() => {
	getList();
	getOption();

});
</script>
<style scoped></style>